/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.imagesteganographylibrary.view;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

import static ohos.agp.animation.Animator.CurveType.LINEAR;

/**
 * uri转pixmap
 *
 * @ProjectName: Image-Steganography-Library
 * @ClassName: CircleView
 * @Description: CircleView
 * @Author: lr
 * @CreateDate: 2021/5/7 15:19
 * @since 2021-05-07
 */
public class CircleView extends Component implements Component.EstimateSizeListener, Component.DrawTask,
        Animator.StateChangedListener, Animator.LoopedListener {
    /**
     * INFINITE
     */
    public static final int INFINITE = -1;
    RectFloat rectF = new RectFloat();
    private static final int strokeWidth = 12;
    private static final int rgbAlpha = 200;
    private static final int rgbRed = 255;
    private static final int rgbGreen = 20;
    private static final int rgbBlue = 147;
    private static final int drawCircle = 2;
    private static final float sweepAngle = 100;
    private static final float padding = 5;
    private static final int angle = 360;
    /**
     * valueAnimator
     */
    private AnimatorValue valueAnimator;
    private Paint mPaint;
    private Paint mPaintPro;

    private float mWidth = 0f;
    private float mPadding = 0f;
    private float startAngle = 0f;


    /**
     * CircleView
     *
     * @param context context
     */
    public CircleView(Context context) {
        this(context, null);
    }

    /**
     * CircleView
     *
     * @param context context
     * @param attrs attrs
     */
    public CircleView(Context context, AttrSet attrs) {
        this(context, attrs, 0);
    }

    /**
     * CircleView
     *
     * @param context context
     * @param attrs attrs
     * @param defStyleAttr defStyleAttr
     */
    public CircleView(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }


    private void initPaint() {
        addDrawTask(this);
        setEstimateSizeListener(this);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE_STYLE);
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(strokeWidth);

        mPaintPro = new Paint();
        mPaintPro.setAntiAlias(true);
        mPaintPro.setStyle(Paint.Style.STROKE_STYLE);
        mPaintPro.setColor(new Color(Color.argb(rgbAlpha, rgbRed, rgbGreen, rgbBlue)));
        mPaintPro.setStrokeWidth(strokeWidth);

    }

    /**
     * setViewColor
     *
     * @param color color
     */
    public void setViewColor(int color) {
        mPaintPro.setColor(new Color(color));
        invalidate();
    }

    /**
     * setBarColor
     *
     * @param color color
     */
    public void setBarColor(int color) {
        mPaint.setColor(new Color(color));
        invalidate();
    }

    /**
     * onDraw
     *
     * @param component Component
     * @param canvas Canvas
     * */
    @Override
    public void onDraw(Component component, Canvas canvas) {
        canvas.drawCircle(mWidth / drawCircle, mWidth / drawCircle, mWidth / drawCircle - mPadding, mPaintPro);
        rectF = new RectFloat(mPadding, mPadding, mWidth - mPadding, mWidth - mPadding);
        canvas.drawArc(rectF, new Arc(startAngle, sweepAngle, false), mPaint);
    }

    /**
     * onEstimateSize
     *
     * @param i1
     * @param i
     * @return boolean
     * */
    @Override
    public boolean onEstimateSize(int i, int i1) {
        if (EstimateSpec.getSize(i) > getHeight()) {
            mWidth = EstimateSpec.getSize(i1);
        } else {
            mWidth = EstimateSpec.getSize(i);
        }
        mPadding = padding;
        return false;
    }



    private AnimatorValue startViewAnim(float startF, final float endF, long time) {
        valueAnimator = new AnimatorValue();
        valueAnimator.setDuration(time);
        valueAnimator.setCurveType(LINEAR);
        valueAnimator.setLoopedCount(INFINITE);

        valueAnimator.setStateChangedListener(this);
        valueAnimator.setLoopedListener(this);
        valueAnimator.setValueUpdateListener((animatorValue, value) -> {
            startAngle = angle * value;

            invalidate();
        });


        if (!valueAnimator.isRunning()) {
            valueAnimator.start();

        }

        return valueAnimator;
    }

    /**
     * startAnim
     *
     * @param time time
     */
    public void startAnim(int time) {
        stopAnim();
        startViewAnim(0f, 1f, time);
    }

    /**
     * stopAnim
     */
    public void stopAnim() {
        if (valueAnimator != null) {
            createAnimatorProperty().reset();
            valueAnimator.setLoopedCount(0);
            valueAnimator.cancel();
            valueAnimator.end();
        }
    }


    @Override
    public void onRepeat(Animator animator) {

    }

    @Override
    public void onStart(Animator animator) {

    }

    @Override
    public void onStop(Animator animator) {

    }

    @Override
    public void onCancel(Animator animator) {

    }

    @Override
    public void onEnd(Animator animator) {

    }

    @Override
    public void onPause(Animator animator) {

    }

    @Override
    public void onResume(Animator animator) {

    }
}
